#!/bin/bash

# _detect_platform

#############################################################################

# Usage: ipmi_cmd "00 0f"
function ipmi_cmd()
{
    nxCMD="$1"
    wxCMD=`echo "$1" | awk '{ for (i=1;i<=NF;i++) printf("0x%s ", $i); printf("\n")}'`
    [ -f /usr/bin/ipmitool ] && /usr/bin/ipmitool raw $wxCMD | awk '{for (i=1;i<=NF;i++) printf("%s ", $i); printf("\n")}' && return
    CMD=`no_x "$1"`
    [ -f /usr/bin/ipmicmd ] && /usr/bin/ipmicmd -k "0f 00 $nxCMD" smi 0 | awk '{for (i=6;i<=NF;i++) printf("%s ", $i); printf("\n")}' && return
}


function linux_vendor()
{
    [ "$linux_rpm" == "" ] && export linux_rpm=`rpm -qf /etc/issue`
    VENDOR=`rpm -q --queryformat='%{NAME}' $linux_rpm | awk -F'-' '{print $1}'`
    [ "$VENDOR" == "sles" ] && VENDOR="suse"
    [ "$VENDOR" == "rhel" ] && VENDOR="redhat"
    echo $VENDOR
}

function distro_version()
{
    [ "$linux_rpm" == "" ] && export linux_rpm=`rpm -qf /etc/issue`
    rpm -q --queryformat='%{VERSION}' $linux_rpm | awk '{gsub("[S|s]erver", "", $0); print $0}'
    #| awk -F '.' '{ gsub(/[a-zA-Z]+$/, "", $1); print $1}'
}

function distro_release()
{
    [ "$linux_rpm" == "" ] && export linux_rpm=`rpm -qf /etc/issue`
    rpm -q --queryformat='%{RELEASE}' $linux_rpm 
}

function linux_distro()
{
    LD_VENDOR=`linux_vendor`
    [ "$LD_VENDOR" == "redhat" ] && LD_VENDOR="rhel"
    [ "$LD_VENDOR" == "suse" ] && LD_VENDOR="sles"
    # Only keep the major version
    LD_VERSION=`distro_version | awk -F'.' '{print $1}'`

    echo "$LD_VENDOR$LD_VERSION"
}

function cim_server()
{
    DISTRO=`linux_distro`
    PEGASUS="pegasus"
    SFCB="sfcb"
    [ "$DISTRO" == "rhel4" ] && echo "$PEGASUS"
    [ "$DISTRO" == "rhel5" ] && echo "$PEGASUS"
    [ "$DISTRO" == "rhel6" ] && echo "$PEGASUS"
    [ "$DISTRO" == "sles10" ] && echo "$PEGASUS"
    [ "$DISTRO" == "sles11" ] && echo "$SFCB"
    [ "$DISTRO" == "sles11.1" ] && echo "$SFCB"
    [ "$DISTRO" == "sles11.2" ] && echo "$SFCB"
}

function cpu_arch()
{
    uname -p | awk '{if ($0 ~ /unkown|x86_64|i386|i586|i686|i686_64/) print "x86"; else print $0}'
}

function cpu_arch_ifil()
{
    uname -p | awk '{if ($0 ~ /unkown|i386|i586|i686/) print "i586"; else print $0}'
}

function os_bits()
{
    file /sbin/init | awk '{for (i=1;i<=NF;i++) if ($i ~ /[0-9]+-bit$/){ print $i; break}}' | awk -F'-' '{print $1}'
}

function check_endian()
{
    CFILE="./_test_endian.c"
    BFILE="./_test_endian"
    echo "#include <stdio.h>" >$CFILE
    echo "int main()" >>$CFILE
    echo "{" >>$CFILE
    echo "int x=1;" >>$CFILE
    echo "if (*(char *) &x ==1) printf(\"Little Endian\n\");" >>$CFILE
    echo "else    printf(\"Big Endian\n\");" >>$CFILE
    echo "return 0;" >>$CFILE
    echo "}" >>$CFILE
    cc $CFILE -o $BFILE
    chmod +x $BFILE
    $BFILE
    rm -f $BFILE
    rm -f $CFILE
}

function bmc_rev()
{
    systool -b platform -A firmware_revision | grep firmware_revision | awk -F'=' '{gsub(" ","",$2); gsub("\"","",$2); print $2}'
}


function sfw_rev()
{
    dmidecode -t 0 | grep "BIOS Revision:" | awk -F':' '{gsub(" ", "", $2); print $2}'
}

function server_type()
{
    stype=`dmidecode  -t 1 | grep "Product Name:" | awk -F':' '{gsub(/^ /, "", $2); print $2}'`
    manuf=`dmidecode  -t 1 | grep "Manufacturer:" | awk -F':' '{gsub(/^ /, "", $2); print $2}'`
    echo $manuf $stype
}

function test()
{
   funcn="$1"
   echo -e "$funcn = \""`$funcn`"\""
}

if [ "$SEA_DBG" != "" ]
then
  test linux_vendor
  test linux_distro
  test distro_version
  test cpu_arch
  test cpu_arch_ifil
  test os_bits
  # test check_endian
  echo ""
  test bmc_rev
  test sfw_rev
  test server_type
fi

#############################################################################
# Pre load functions

